package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.*;
import com.itheima.reggie.mapper.AddressMapper;
import com.itheima.reggie.mapper.OrderDetailMapper;
import com.itheima.reggie.mapper.OrderMapper;
import com.itheima.reggie.service.CartService;
import com.itheima.reggie.service.OrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private CartService cartService;

    @Autowired
    private AddressMapper addressMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Override
    public void save(Order order) {
        //1. 获取当前用户
        User user = UserHolder.get();

        //2. 查询当前用户的购物车
        List<Cart> cartList = cartService.findList();

        //3. 根据提交的addressId,查询用户的收货地址信息
        Address address = addressMapper.selectById(order.getAddressId());

        //4. 组装订单详情数据,保存订单详情表
        //4-1  先确定下订单的主键
        Long orderId = IdWorker.getId();

        //4-2 组装订单详情
        BigDecimal amount = new BigDecimal(0);
        for (Cart cart : cartList) {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setName(cart.getName());
            orderDetail.setOrderId(orderId);
            orderDetail.setDishId(cart.getDishId());
            orderDetail.setSetmealId(cart.getSetmealId());
            orderDetail.setDishFlavor(cart.getDishFlavor());
            orderDetail.setNumber(cart.getNumber());
            orderDetail.setAmount(cart.getAmount());
            orderDetail.setImage(cart.getImage());

            amount = amount.add(cart.getAmount().multiply(new BigDecimal(cart.getNumber())));

            orderDetailMapper.insert(orderDetail);
        }
        //5. 组装订单数据,保存订单表
        order.setId(orderId);
        order.setNumber(String.valueOf(orderId));
        order.setStatus(1);
        order.setUserId(user.getId());
        order.setAddressId(address.getId());
        order.setOrderTime(new Date());
        order.setCheckoutTime(new Date());
        order.setAmount(amount);
        order.setUserName(user.getName());
        order.setPhone(address.getPhone());
        StringBuilder bd = new StringBuilder();//拼接具体地址
        bd.append(StringUtils.isNotEmpty(address.getProvinceName()) ? address.getProvinceName() : "");
        bd.append(StringUtils.isNotEmpty(address.getCityName()) ? address.getCityName() : "");
        bd.append(StringUtils.isNotEmpty(address.getDistrictName()) ? address.getDistrictName() : "");
        bd.append(StringUtils.isNotEmpty(address.getDetail()) ? address.getDetail() : "");
        order.setAddress(bd.toString());
        order.setConsignee(address.getConsignee());
        orderMapper.insert(order);

        //注意: 企业中大多情况下,先一表,再来多表,因为多表中会用到一表的主键

        //6. 清空购物车
        cartService.clean();
    }

    @Override
    public Page<Order> findByPage(Integer pageNum, Integer pageSize, Date beginTime, Date endTime, Long number) {

        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(number != null, Order::getId, number);
        wrapper.ge(beginTime != null, Order::getOrderTime, beginTime);
        wrapper.ge(endTime != null, Order::getOrderTime, endTime);

        //时间排序
        wrapper.orderByDesc(Order::getOrderTime);
        Page<Order> page = new Page<>(pageNum, pageSize);

        page = orderMapper.selectPage(page, wrapper);

        List<Order> orderList = page.getRecords();
        if (CollectionUtils.isNotEmpty(orderList)) {
            for (Order order : orderList) {
                //根据订单id从详情表中获取详情信息
                LambdaQueryWrapper<OrderDetail> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(OrderDetail::getOrderId, order.getId());
                List<OrderDetail> orderDetailList = orderDetailMapper.selectList(wrapper1);
                order.setDetails(orderDetailList);

            }
        }

        return page;
    }

}
